pipeline {
    agent any

    environment {
        // 定义环境变量
        server_name = "yudao-ui"
        port = 9000
        imagename = "${server_name}:${BUILD_NUMBER}"
    }



    stages {

        stage('清理工作区') {
             steps {
                 // 清理工作区 删除之前的构建文件和Git引用
                 //cleanWs()
                 // 如果cleanWs()不可用 可以使用以下命令
                 sh 'rm -rf .git/logs/refs/remotes/origin/feature'
                 //sh 'rm -rf .git/refs/remotes/origin/feature/*'
                 //sh 'rm -rf .git/logs/refs/remotes/origin/feature/bpm'
                 //sh 'rm -rf ./*'
             }
         }

        stage('拉取代码') {
            steps {
                checkout([$class: 'GitSCM',
                    branches: [[name: '*/test']],
                    userRemoteConfigs: [[
                        url: 'http://192.168.110.249:7002/audit-plat/audit-plat-web.git',
                        credentialsId: 'b2cb23f7-1290-427d-83f5-78375ac90339'
                    ]]
                ])
            }
        }

        stage('初始化') {
            steps {
                // 使用 Node.js 镜像安装依赖
                sh 'npm config set registry https://registry.npmmirror.com/'
                sh 'node -v'
                sh 'npm install'
            }
        }



        stage('构建') {
            steps {
                // 构建 Vue3 项目
                sh 'npm run build:test'
            }
        }

         stage('部署到服务器') {
            steps {
                sh '''
                # 将目标目录权限开放给 Jenkins 用户
                    // sudo chown -R jenkins:jenkins /home/data/www
                    rm -rf /home/data/www/dist-test/*
                    cp -r dist-test/* /home/data/www/dist-test/
                '''
            }
        }
    }
}